package com.ttn.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ttn.business.service.CourseUnitOralPracticeService;
import com.ttn.common.core.entity.PageQuery;
import com.ttn.common.core.entity.Result;
import com.ttn.common.core.entity.business.CourseUnitOralPractice;
import com.ttn.common.core.entity.business.bo.ModifyCourseUnitOralPracticeSortBO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * (course_unit_oral_practice)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/course-unit-oral-practice")
@Slf4j
public class CourseUnitOralPracticeController {
    /**
     * 服务对象
     */
    @Resource
    private CourseUnitOralPracticeService courseUnitOralPracticeService;

    /**
     * 保存语法口语跟读
     *
     * @param request
     * @param courseUnitOralPractice
     * @return
     */
    @PostMapping("/save/course-unit-oral-practice")
    public Result saveCourseUnitOralPractice(HttpServletRequest request, @RequestBody CourseUnitOralPractice courseUnitOralPractice) {
        log.info("CourseUnitOralPracticeController saveCourseUnitOralPractice ,参数:{}", courseUnitOralPractice);
        LambdaQueryWrapper<CourseUnitOralPractice> courseUnitOralPracticeLambdaQueryWrapper = new LambdaQueryWrapper();
        courseUnitOralPracticeLambdaQueryWrapper.eq(CourseUnitOralPractice::getCourseUnitId, courseUnitOralPractice.getCourseUnitId());
        courseUnitOralPractice.setSort(Math.toIntExact(courseUnitOralPracticeService.count(courseUnitOralPracticeLambdaQueryWrapper)) + 1);
        courseUnitOralPracticeService.save(courseUnitOralPractice);
        return Result.success();
    }

    /**
     * 修改口语跟读
     *
     * @param request
     * @param courseUnitOralPractice
     * @return
     */
    @PutMapping("/modify/course-unit-oral-practice")
    public Result modifyCourseUnitOralPractice(HttpServletRequest request, @RequestBody CourseUnitOralPractice courseUnitOralPractice) {
        log.info("CourseUnitOralPracticeController saveCourseUnitOralPractice ,参数:{}", courseUnitOralPractice);
        LambdaQueryWrapper<CourseUnitOralPractice> courseUnitOralPracticeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseUnitOralPracticeLambdaQueryWrapper.eq(CourseUnitOralPractice::getFollowText, courseUnitOralPractice.getFollowText());
        courseUnitOralPracticeLambdaQueryWrapper.eq(CourseUnitOralPractice::getCourseUnitId,
                courseUnitOralPractice.getCourseUnitId());
        courseUnitOralPracticeLambdaQueryWrapper.ne(CourseUnitOralPractice::getId, courseUnitOralPractice.getId());
        if (courseUnitOralPracticeService.count(courseUnitOralPracticeLambdaQueryWrapper) > 0) {
            return Result.fail("单元内口语跟读内容重复,请确认后重试");
        }
        courseUnitOralPracticeService.updateById(courseUnitOralPractice);
        return Result.success();
    }

    /**
     * 修改口语跟读排序
     *
     * @param request
     * @param modifyCourseUnitOralPracticeSortBO
     * @return
     */
    @PutMapping("/modify/course-unit-oral-practice-sort")
    public Result modifyCourseUnitOralPracticeSort(HttpServletRequest request, @RequestBody ModifyCourseUnitOralPracticeSortBO modifyCourseUnitOralPracticeSortBO) {
        log.info("CourseUnitOralPracticeController modifyCourseUnitOralPracticeSort ,参数:{}", modifyCourseUnitOralPracticeSortBO);
        CourseUnitOralPractice courseUnitOralPractice = courseUnitOralPracticeService.getById(modifyCourseUnitOralPracticeSortBO.getId());
        LambdaQueryWrapper<CourseUnitOralPractice> courseUnitOralPracticeLambdaQueryWrapper = new LambdaQueryWrapper();
        courseUnitOralPracticeLambdaQueryWrapper.ne(CourseUnitOralPractice::getId, modifyCourseUnitOralPracticeSortBO.getId());
        courseUnitOralPracticeLambdaQueryWrapper.eq(CourseUnitOralPractice::getCourseUnitId,
                courseUnitOralPractice.getCourseUnitId());
        courseUnitOralPracticeLambdaQueryWrapper.orderByAsc(CourseUnitOralPractice::getSort);
        List<CourseUnitOralPractice> courseUnitOralPracticeList = courseUnitOralPracticeService.list(courseUnitOralPracticeLambdaQueryWrapper);
        if (courseUnitOralPracticeList.size() > 0) {
            int sortIndex = 0;
            if (modifyCourseUnitOralPracticeSortBO.getSortingBehavior().equals(-1)) {
                if (courseUnitOralPractice.getSort() - 2 > 0) {
                    sortIndex = courseUnitOralPractice.getSort() - 2;
                }
            } else if (modifyCourseUnitOralPracticeSortBO.getSortingBehavior().equals(1)) {
                sortIndex = courseUnitOralPractice.getSort();
            }
            courseUnitOralPracticeList.add(sortIndex, courseUnitOralPractice);
            AtomicInteger i = new AtomicInteger(1);
            courseUnitOralPracticeList.stream().forEach(courseUnitGrammarTest1 -> {
                courseUnitGrammarTest1.setSort(i.getAndIncrement());
            });
            courseUnitOralPracticeService.updateBatchById(courseUnitOralPracticeList);
        }
        return Result.success();
    }

    /**
     * 删除口语跟读项
     *
     * @param request
     * @param id
     * @return
     */
    @DeleteMapping("/delete/course-unit-oral-practice-by-id/{id}")
    public Result deleteCourseUnitOralPracticeById(HttpServletRequest request, @PathVariable("id") Integer id) {
        log.info("CourseUnitOralPracticeController deleteCourseUnitOralPracticeById ,参数id:{}", id);
        CourseUnitOralPractice courseUnitOralPractice = courseUnitOralPracticeService.getById(id);
        courseUnitOralPracticeService.removeById(id);
        LambdaQueryWrapper<CourseUnitOralPractice> courseUnitOralPracticeLambdaQueryWrapper = new LambdaQueryWrapper();
        courseUnitOralPracticeLambdaQueryWrapper.ne(CourseUnitOralPractice::getId, id);
        courseUnitOralPracticeLambdaQueryWrapper.eq(CourseUnitOralPractice::getCourseUnitId, courseUnitOralPractice.getCourseUnitId());
        courseUnitOralPracticeLambdaQueryWrapper.orderByAsc(CourseUnitOralPractice::getSort);
        List<CourseUnitOralPractice> courseUnitOralPracticeList = courseUnitOralPracticeService.list(courseUnitOralPracticeLambdaQueryWrapper);
        if (courseUnitOralPracticeList.size() > 0) {
            AtomicInteger i = new AtomicInteger(1);
            courseUnitOralPracticeList.stream().forEach(courseUnitGrammarTest1 -> {
                courseUnitGrammarTest1.setSort(i.getAndIncrement());
            });
            courseUnitOralPracticeService.updateBatchById(courseUnitOralPracticeList);
        }
        return Result.success();
    }

    @PostMapping("/query/course-unit-oral-practice-by-course-unit-id-page/{courseUnitId}")
    public Result queryCourseUnitOralPracticeByCourseUnitIdPage(HttpServletRequest request, @PathVariable("courseUnitId") Integer courseUnitId, @RequestBody PageQuery pageQuery) {
        log.info("CourseUnitOralPracticeController queryCourseUnitOralPracticeByCourseUnitIdPage ,参数pageQuery:{},courseUnitId:{}", pageQuery, courseUnitId);
        IPage<CourseUnitOralPractice> courseUnitOralPracticeIPage = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        LambdaQueryWrapper<CourseUnitOralPractice> courseUnitLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseUnitLambdaQueryWrapper.eq(CourseUnitOralPractice::getCourseUnitId, courseUnitId);
        courseUnitLambdaQueryWrapper.orderByAsc(CourseUnitOralPractice::getSort);
        return Result.success(courseUnitOralPracticeService.page(courseUnitOralPracticeIPage, courseUnitLambdaQueryWrapper));
    }

    @PostMapping("/service/query/course-unit-oral-practice-by--course-unit-id/{courseUnitId}")
    public Result serviceQueryCourseUnitOralPracticeByCourseUnitId(@PathVariable(
            "courseUnitId") Integer courseUnitId) {
        log.info("CourseUnitOralPracticeController serviceQueryCourseUnitOralPracticeByCourseUnitId ,参数courseUnitId:{}",
                courseUnitId);
        LambdaQueryWrapper<CourseUnitOralPractice> courseUnitOralPracticeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseUnitOralPracticeLambdaQueryWrapper.eq(CourseUnitOralPractice::getCourseUnitId, courseUnitId);
        return Result.success(courseUnitOralPracticeService.count(courseUnitOralPracticeLambdaQueryWrapper));
    }
}
